﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crude.Common;
using System.IO;
using System.Runtime.Serialization;

namespace Crude.Core
{
    public class ProjectObjectModelLoader
    {
        string m_pomFile;

        public ProjectObjectModelLoader(string pomFile)
        {
            m_pomFile = pomFile;
        }

        public ProjectObjectModel Load()
        {
            if (string.IsNullOrEmpty(m_pomFile))
            {
                throw new ArgumentException("Pom file is empty");
            }
            else if (!File.Exists(m_pomFile))
            {
                throw new FileNotFoundException("Pom file doesn't exists");
            }
            else
            {
                using (FileStream fileStream = new FileStream(m_pomFile, FileMode.Open, FileAccess.Read))
                {
                    DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(ProjectObjectModel));
                    ProjectObjectModel pom = (ProjectObjectModel)dataContractSerializer.ReadObject(fileStream);

                    return pom;
                }             
            }
        }
    }
}
